package com.thinkersre.actuator;

import com.thinkersre.util.ThinkersReUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

@Component
@Endpoint(id="database")
public class DatabaseEndpoint {
    private static final Logger logger = LoggerFactory.getLogger(DatabaseEndpoint.class);
    @Resource
    private DataSource dataSource;
    @ReadOperation//只能通过get请求访问
    public String checkConnection(){
            try (Connection connection = dataSource.getConnection()) {
                return ThinkersReUtil.getJSONString(0,"获取连接成功");
        } catch (SQLException e) {
                logger.error("获取连接失败:" + e.getMessage());
                return ThinkersReUtil.getJSONString(1,"获取连接失败");
        }
    }
}
